package com.feng.controller;

import com.feng.annotation.SystemLog;
import com.feng.domain.ResponseResult;
import com.feng.domain.dto.LoginUserDto;
import com.feng.domain.entity.User;
import com.feng.enums.AppHttpCodeEnum;
import com.feng.exception.SystemException;
import com.feng.service.LoginService;
import com.feng.utils.BeanCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Api(tags = "登录和登出")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginUserDto loginUserDto){
        User user = BeanCopyUtil.articleVo(loginUserDto, User.class);
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.USERNAME_NOT_NULL);
        }
        return loginService.login(user);
    }

    @ApiOperation(value = "登出")
    @PostMapping("loginOut")
    public ResponseResult loginOut(){
        return loginService.loginOut();
    }
}
